package com.yutel.google;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;

import cn.com.xinli.android.service.protocol.AppOrderProduct;
import cn.com.xinli.android.service.protocol.AppOrderProduct.QueryAppProductReponse.AppProduct;

public class TestAppOrderProduct {
	private String AUTHIPADRESS = "192.168.2.243";
	private int connTimeOut = 2000;
	private int dataTimeOut = 3000;

	public static void main(String[] arg) {
		String account = "999967343463";
		int appcode = 21;
		int cuxiaoid = 1;
		int productid = 1;
		String card = "9442427325624547";
		TestAppOrderProduct t = new TestAppOrderProduct();
		t.testQueryAppRequest(account, appcode);
//		t.testQueryAppProductRequest(account, appcode, cuxiaoid);
//		t.testOrderAppProductRequest(account, appcode, cuxiaoid, productid, card);
	}

	/**
	 * 查询应用信息
	 * 
	 * @param account
	 * @param channelid
	 * @param videoName
	 */
	public void testQueryAppRequest(String account, int appCode) {
		Socket socketClient;
		DataOutputStream dataOutput;
		DataInputStream dataInput = null;
		try {
			socketClient = new Socket();
			socketClient.connect(new InetSocketAddress(AUTHIPADRESS, 5258),
					connTimeOut);
			socketClient.setSoTimeout(dataTimeOut);
			dataOutput = new DataOutputStream(socketClient.getOutputStream());
			AppOrderProduct.QueryAppRequest.Builder builder = AppOrderProduct.QueryAppRequest
					.newBuilder();
			builder.setUserId(account);
			builder.setAppCode(appCode);
			AppOrderProduct.QueryAppRequest querApp = builder.build();
			dataOutput.writeInt(querApp.getSerializedSize());// 传输序列化的字节长度
			dataOutput.write(querApp.toByteArray());// 将发送内容转换为字节码数组
			dataOutput.flush();// 清空缓存
			dataInput = new DataInputStream(socketClient.getInputStream());
			int length = dataInput.readInt();// 获取返回值的长度
			System.out.println("length=" + length);
			byte dataini[] = new byte[length];// 设置长度
			dataInput.readFully(dataini);
			dataInput.close();
			socketClient.close();
			AppOrderProduct.QueryAppReponse res = AppOrderProduct.QueryAppReponse
					.parseFrom(dataini);
			if (res != null) {
				System.out.println("getResultCode=" + res.getResultCode()
						+ ",getResultMessage=" + res.getResultMessage());
				System.out.println("name=" + res.getName() + ",info="
						+ res.getInfo());
				System.out.println("statu=" + res.getUserStatus() + ",day="
						+ res.getDays() + ",re_day=" + res.getRemindDays());
				System.out.println("cuxiao=" + res.getCuxiao().getName()
						+ ",free_cuxiao=" + res.getFreeCuxiao().getName());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 查询产品
	 * 
	 * @param account
	 * @param appCode
	 * @param cuxiaoid
	 */
	public void testQueryAppProductRequest(String account, int appCode,
			int cuxiaoid) {
		Socket socketClient;
		DataOutputStream dataOutput;
		DataInputStream dataInput = null;
		try {
			socketClient = new Socket();
			socketClient.connect(new InetSocketAddress(AUTHIPADRESS, 5255),
					connTimeOut);
			socketClient.setSoTimeout(dataTimeOut);
			dataOutput = new DataOutputStream(socketClient.getOutputStream());
			AppOrderProduct.QueryAppProductRequest.Builder builder = AppOrderProduct.QueryAppProductRequest
					.newBuilder();
			builder.setUserId(account);
			builder.setAppCode(appCode);
			builder.setCuxiaoId(cuxiaoid);
			AppOrderProduct.QueryAppProductRequest keywordSearch = builder
					.build();
			dataOutput.writeInt(keywordSearch.getSerializedSize());// 传输序列化的字节长度
			dataOutput.write(keywordSearch.toByteArray());// 将发送内容转换为字节码数组
			dataOutput.flush();// 清空缓存
			dataInput = new DataInputStream(socketClient.getInputStream());
			int length = dataInput.readInt();// 获取返回值的长度
			System.out.println("length=" + length);
			byte dataini[] = new byte[length];// 设置长度
			dataInput.readFully(dataini);
			dataInput.close();
			socketClient.close();
			AppOrderProduct.QueryAppProductReponse res = AppOrderProduct.QueryAppProductReponse
					.parseFrom(dataini);
			if (res != null) {
				System.out.println("getResultCode=" + res.getResultCode()
						+ ",getResultMessage=" + res.getResultMessage());
				for (int i = 0; i < res.getAppProductListCount(); i++) {
					AppProduct ap = res.getAppProductListList().get(i);
					System.out.println("name=" + ap.getName() + ",info="
							+ ap.getInfo() + ",price=" + ap.getPrice());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 产品订购
	 * @param account
	 * @param appCode
	 * @param cuxiaoid
	 * @param productid
	 * @param card
	 */
	public void testOrderAppProductRequest(String account, int appCode,
			int cuxiaoid, int productid, String card) {
		Socket socketClient;
		DataOutputStream dataOutput;
		DataInputStream dataInput = null;
		try {
			socketClient = new Socket();
			socketClient.connect(new InetSocketAddress(AUTHIPADRESS, 5256),
					connTimeOut);
			socketClient.setSoTimeout(dataTimeOut);
			dataOutput = new DataOutputStream(socketClient.getOutputStream());
			AppOrderProduct.OrderAppProductRequest.Builder builder = AppOrderProduct.OrderAppProductRequest
					.newBuilder();
			builder.setUserId(account);
			builder.setAppCode(appCode);
			builder.setCuxiaoCode(cuxiaoid);
			builder.setAppProductCode(productid);
			builder.setCardPsw(card);

			AppOrderProduct.OrderAppProductRequest keywordSearch = builder
					.build();
			dataOutput.writeInt(keywordSearch.getSerializedSize());// 传输序列化的字节长度
			dataOutput.write(keywordSearch.toByteArray());// 将发送内容转换为字节码数组
			dataOutput.flush();// 清空缓存
			dataInput = new DataInputStream(socketClient.getInputStream());
			int length = dataInput.readInt();// 获取返回值的长度
			System.out.println("length=" + length);
			byte dataini[] = new byte[length];// 设置长度
			dataInput.readFully(dataini);
			dataInput.close();
			socketClient.close();
			AppOrderProduct.OrderAppProductReponse res = AppOrderProduct.OrderAppProductReponse
					.parseFrom(dataini);
			if (res != null) {
				System.out.println("getResultCode=" + res.getResultCode()
						+ ",getResultMessage=" + res.getResultMessage());
				System.out.println("cardnum=" + res.getCardNum() + ",endtime="
						+ res.getStandardEndTime() + ",free_endtime="
						+ res.getFavorableEndTime());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}